﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Input = Microsoft.Xna.Framework.Input;

namespace BicycleTrafficSimulator.Business
{
    class KeyboardHandler
    {
        public Game game; 
        public KeyboardHandler(Game game)
        {
            this.game = game;
        }

        public void Update(GameTime gameTime){

            float leftRightRot = 0;

            float turningSpeed = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
            turningSpeed *= 1.6f * this.game.GameSpeed;
            Input::KeyboardState keys = Input::Keyboard.GetState();

            if (keys.IsKeyDown(Input::Keys.Right))
            {
                leftRightRot -= turningSpeed;
                Quaternion additionalRotR = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), leftRightRot);
                this.game.CameraRotation *= additionalRotR;
                float moveSpeedR = gameTime.ElapsedGameTime.Milliseconds / 500.0f * this.game.GameSpeed;
                //Vector3 addVectorR = Vector3.Transform(new Vector3(1, 0, 0), xwingRotation);
                //xwingPosition += addVectorR * moveSpeedR;

                this.game.Bike.Rotation -= (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.182f);

            }
            if (keys.IsKeyDown(Input::Keys.Left))
            {
                leftRightRot += turningSpeed;
                this.game.Bike.Rotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.091f);
            }

            Quaternion additionalRotL = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), leftRightRot);
            this.game.CameraRotation *= additionalRotL;
            float moveSpeedL = gameTime.ElapsedGameTime.Milliseconds / 500.0f * this.game.GameSpeed;

            if (keys.IsKeyDown(Input::Keys.Up))
            {
                Vector3 addVectorU = Vector3.Transform(new Vector3(0, 0, -1), this.game.CameraRotation);
                float moveSpeed = gameTime.ElapsedGameTime.Milliseconds / 500.0f * this.game.GameSpeed;
                //   xwingPosition += addVectorU * moveSpeed;
                this.game.SpeedUpDownValue += 0.0005f;
                this.game.Bike.Position += addVectorU * moveSpeed;
            }

            //check for new direction
            //if (Math.Abs(bike.Rotation - previousModelRotation) > MathHelper.PiOver4 + MathHelper.PiOver4 / 2)
            //{
            //    if (bike.Rotation > previousModelRotation) { dirIndex--; previousModelRotation += MathHelper.PiOver2; }
            //    else { dirIndex++; previousModelRotation -= MathHelper.PiOver2; }

            //    if (dirIndex == 4) dirIndex = 0;
            //    if (dirIndex == -1) dirIndex = 3;

            //    Console.WriteLine("new dir " + directions[dirIndex] + bike.Rotation + " " + previousModelRotation);
            //    previousModelRotation += MathHelper.PiOver2;
            //}


            //exiting the game
            if (keys.IsKeyDown(Input::Keys.Escape))
            {
                //try
                //{
                //    kinect.Stop();
                //}
                //catch (Exception e)
                //{

                //}

                // kinect.stop();
                this.game.scene.Exit();
            }

    }
    }
}
